package com.example.demo.elasticsearch;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SourceFilter;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 10:45 2019/8/9
 */
public class EsSearch {
    @Autowired
    private EsRepository esRepository;
    @Autowired
    private ElasticsearchTemplate template;
     //搜索
    public void Search(){
        //构建基础查询器
        NativeSearchQueryBuilder queryBuilder=new NativeSearchQueryBuilder();
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("",""));
        //过滤
         //.filter(QueryBuilders.rangeQuery().gt().lt())
        queryBuilder.withQuery(boolQueryBuilder);
        //查询结果过滤
        queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{"fields.document_type","source","@timestamp"},new String[]{}));
        queryBuilder.withSort(SortBuilders.fieldSort("_score").order(SortOrder.ASC));
        //分页查询
        queryBuilder.withPageable(PageRequest.of(0,2000));
        //查询结果
        Page<Goods> search = esRepository.search(queryBuilder.build());
        //解析结果
        List<Goods> goodsList = search.getContent();
    }
    //聚合
    public void aggs(){
        NativeSearchQueryBuilder queryBuilder=new NativeSearchQueryBuilder();
        //构建聚合条件
        queryBuilder.addAggregation(AggregationBuilders.terms("aggs").field("id").size(0));
        //聚合查询
        AggregatedPage<Goods> aggregatedPage = template.queryForPage(queryBuilder.build(), Goods.class);
        //得到聚合
        Aggregations aggregations = aggregatedPage.getAggregations();
        //指定聚合桶
        StringTerms stringTerms = aggregations.get("id");
        //解析桶
        List<StringTerms.Bucket> buckets = stringTerms.getBuckets();
        for (StringTerms.Bucket bucket : buckets) {
            String keyAsString = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
        }
    }
    //导入es


}
